<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库存组织" prop="orgguid">
        <el-select v-model="queryParams.orgguid" @change="findSm" placeholder="请选择库存组织" filterable clearable size="small">
          <el-option
            v-for="item in orgOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库房" prop="storermguid">
        <el-select v-model="queryParams.storermguid" @change="findStoreroomhouse" placeholder="请选择库房" filterable clearable>
          <el-option
            v-for="item in roomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="库间" prop="storehguid">
        <el-select v-model="queryParams.storehguid" placeholder="请选择库间" filterable clearable>
          <el-option
            v-for="item in storehouseroomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="spectype" >
        <el-select v-model="queryParams.spectype" placeholder="请选择产品类型" size="small" clearable>
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品" prop="productid">
        <el-select v-model="queryParams.productid" placeholder="请选择产品" filterable clearable size="small">
          <el-option
            v-for="item in productOptions"
            :key="item.guid"
            :label="item.productname"
            :value="item.guid"
            @click.native="getSp($event, item)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="规格" prop="spguid">
        <el-select v-model="queryParams.spguid" placeholder="请选择规格" filterable size="small" clearable>
          <el-option
            v-for="item in spOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:whinfo:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="whinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库存组织" align="center" prop="orgname" />
      <el-table-column label="库房" align="center" prop="storename" width="150" />
      <el-table-column label="库间" align="center" prop="storeroomName" />
      <el-table-column label="产品" align="center" prop="productname" />
      <el-table-column label="规格" align="center" prop="specName"  width="250" />
      <el-table-column label="数量" align="center" prop="storeqty" />
      <el-table-column label="单位" align="center" prop="stockunit" />
      <el-table-column label="单价" align="center" prop="price" v-if="showprice"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWhinfo, getWhinfo, exportWhinfo, findDetailPC } from "@/api/bill/whinfo";
import {findOrganizationList, findProductList, findStoreroom, findStorehouse, findSpecificationList,showPrice} from "@/api/public";

export default {
  name: "Whinfo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存信息表格数据
      whinfoList: [],
      showprice:false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storermguid: null,
        storehguid: null,
        productname: null,
        orgguid: null,
        type: 1,
        storeguid: '',
        spectype: '',
        spguid: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      roomOptions: [],
      // 库间管理表格数据
      storehouseroomOptions: [],
      productOptions: [],
      orgOptions: [],
      typeOptions: [],
      querySh: {
        storeguid: '',
      },
      querySm: {
        orgguid: '',
      },
      spOptions: []
    };
  },
  created() {
    this.getShowPrice();
    this.getList();

    // this.findStoreroom();
    this.findProductList();
    this.findOrganizationList();
    this.getDicts("sp_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    getShowPrice(){
      let data={
        id:40
      }
      showPrice(data).then(response=>{
        if (response.code==200){
          this.showprice=response.msg==1;
        }
      })
    },
    getSp(event, item){
      findSpecificationList({productguid:item.guid}).then(response => {
        this.spOptions = response.data;
      });
    },
    /** 查询库间信息列表 */
    findStoreroom(){
      findStoreroom().then(response => {
        this.roomOptions = response.data;
      });
    },
    /** 查询库间信息列表 */
    findSm(query) {
      this.querySm.orgguid = query;
      findStoreroom(this.querySm).then(response => {
        this.roomOptions = response.data;
      });
    },
    /** 查询库间信息列表 */
    findStoreroomhouse(query) {
      this.querySh.storeguid = query;
      findStorehouse(this.querySh).then(response => {
        this.storehouseroomOptions = response.data;
      });
    },
    /** 查询产品信息列表 */
    findProductList(){
      findProductList().then(response => {
        this.productOptions = response.data;
      });
    },
    //查询组织
    findOrganizationList(){
      findOrganizationList().then(response => {
        this.orgOptions = response.data;
      });
    },
    /** 查询库存信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = 1;
      findDetailPC(this.queryParams).then(response => {
        this.whinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        storermguid: null,
        batchid: null,
        storehguid: null,
        storehcode: null,
        productid: null,
        productname: null,
        storeqty: null,
        stockunit: null,
        price: null,
        wistartdate: null,
        wienddate: null,
        wostartdate: null,
        woenddate: null,
        createdate: null,
        operatorid: null,
        lastupdate: null,
        orgguid: null,
        spguid: null,
        stockunitid: null,
        autoid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.autoid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const autoid = row.autoid || this.ids
      getWhinfo(autoid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.autoid != null) {
            updateWhinfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWhinfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const autoids = row.autoid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWhinfo(autoids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有库存信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWhinfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
